﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SellItemItem : CommodityBase
{
    public Text m_NameT;
    public Text m_PriceT;
    public Text m_Number;
    public Image m_Perview;

    public Callback OnSell;
    public Item Item {
        set {
            if (null == value)
                return;
            m_CommodityItem = value;
            m_Number.text = m_CommodityItem.m_Number.ToString();
            m_NameT.text = m_CommodityItem.Name;
            m_PriceT.text = ((int)(UtilsGame.GetPrice(m_CommodityItem))).ToString();
            m_Perview.sprite = Resources.Load<Sprite>(m_CommodityItem.Perview);
            m_Perview.preserveAspect = true;
        }
        get { return m_CommodityItem; }
    }
    Item m_CommodityItem;
    public void OnSellClicked()
    {
        int number = 1;
        if (Input.GetKey(KeyCode.LeftShift))
            number = Mathf.Min(10, m_CommodityItem.m_Number);
        number *= (int)Mathf.Clamp(m_DownAllCount, 1, 10);
        number = Mathf.Min(number, m_CommodityItem.m_Number);
        if (m_CommodityItem.m_Number <= number)
        {
            Destroy(this.gameObject);
        }
        Player.Instance.removeItem(m_CommodityItem.m_Type, number);
        m_Number.text = m_CommodityItem.m_Number.ToString();
        Player.Instance.Money += (int)UtilsGame.GetPrice(m_CommodityItem) * number;
        if (OnSell != null)
            OnSell();
    }
    override protected void Update()
    {
        base.Update();

        if (m_IsDown)
        {
            m_DownTimeCount += Time.deltaTime;
            m_DownAllCount += Time.deltaTime;
            m_DownAllCount = Mathf.Min(m_DownAllCount, 3);
        }
        else
        {
            m_DownTimeCount = 0;
            m_DownAllCount = 0;
        }

        if (m_DownTimeCount > 0.7f)
        {
            OnSellClicked();
            m_DownTimeCount -= Mathf.Max((0.3f - (m_DownAllCount / 3) * 0.3f), 0.03f) ;
        }
    }
    protected override void showDescritionPanel()
    {
        base.showDescritionPanel();
        if (m_DescriptionPanel == null)
            return;
        WeaponDescriptionPanel panel = m_DescriptionPanel.GetComponent<WeaponDescriptionPanel>();
        panel.PerviewWeapon = m_CommodityItem as Weapon;
    }
    float m_DownTimeCount = 0;
    bool m_IsDown = false;
    float m_DownAllCount = 0;
    public void OnDown()
    {
        m_IsDown = true;
    }
    public void OnUp()
    {
        m_IsDown = false;
    }
}
